<template>
  <div>
    <el-form label-width="80px"
             :model="orderInfo"
             :rules="rules"
             ref="fieldaddfield">

      <template v-for="(items,index) in addfields">
        <template v-if="items.type==='htmltext'">
          <el-form-item :label="items.itemname"
                        :key='index'>
            <editor :catchData="orderInfo[items.TagName]">
            </editor>
          </el-form-item>
        </template>
        <template v-else-if="items.type==='radio'">
          <el-form-item :label="items.itemname"
                        :key="index">
            <template v-for="(ridios,index) in getArr(items.default)">
              <el-radio v-model="orderInfo[items.TagName]"
                        :label="ridios"
                        :key="index">{{ridios}}</el-radio>
            </template>
          </el-form-item>
        </template>
        <template v-else-if="items.type==='multitext'">
          <el-form-item :label="items.itemname"
                        :key="index">
            <el-input type="textarea"
                      v-model="orderInfo[items.TagName]"
                      :maxlength="items.maxlength"
                      show-word-limit
                      rows='4'
                      placeholder="请输入内容"></el-input>
          </el-form-item>
        </template>

        <template v-else-if="items.type==='imgfile'">
          <el-form-item :label="items.itemname"
                        class="img-litpic"
                        :key="index">
            <div class="demo-image__preview"
                 v-if="orderInfo[items.TagName]">
              <el-image fit="fit"
                        :src="orderInfo[items.TagName]"
                        class="avatar"
                        :preview-src-list="[orderInfo[items.TagName]]">
              </el-image>
            </div>
            <el-input type="text"
                      class="img-input"
                      v-model="orderInfo[items.TagName]">
            </el-input>
            <el-upload class="avatar-uploader"
                       :action="imguploadurl"
                       :show-file-list="false"
                       :on-success="handleAvatarSuccess.bind(this,items.TagName)"
                       :before-upload="beforeAvatarUpload">

              <el-button class="img-up"
                         type="primary">上传<i class="el-icon-upload el-icon--right"></i></el-button>

            </el-upload>
          </el-form-item>
        </template>

        <template v-else-if="items.type==='int'">
          <el-form-item :label="items.itemname"
                        :key="index">
            <el-input-number v-model="orderInfo[items.TagName]"
                             placeholder="请输入数字"></el-input-number>
          </el-form-item>
        </template>

        <template v-else>
          <el-form-item :label="items.itemname"
                        :key="index">

            <el-input v-model="orderInfo[items.TagName]"
                      placeholder="请输入内容"></el-input>
          </el-form-item>
        </template>
      </template>
    </el-form>
  </div>
</template>

<script>
import editor from '../editor/wangeditor'
import urls from '../../axios/url'
export default {
  data () {
    return {
      imguploadurl: urls.imgupload,
      orderInfo: {

      },
      rules: {
        fjtitle: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ]
      }
    }
  },
  components: { editor },
  props: ['addfields', 'dede_addonfieldval'],
  mounted () {
    this.orderInfo = this.dede_addonfieldval
  },
  methods: {
    handleAvatarSuccess (idx, res, file, name) {
      if (res.state === 1) {
        this.$msg(res.msg, 1)
        this.orderInfo[idx] = res.data
      } else {
        this.$msg(res.msg, 3)
      }
    },
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    getData () {
      return this.orderInfo
    },
    getArr (arr) { // 把以逗号内容改成数组
      return arr.split(',')
    },
    resetForm (formName) {
      this.$refs['fieldaddfield'].resetFields()
    }
  },
  watch: {
    'dede_addonfieldval': function (newVal, oldVal) {
      console.log(newVal)
      console.log(oldVal)
      if (newVal) {
        this.orderInfo = newVal
      }
    }
  }
}
</script>

<style lang='less' scoped>
.img-litpic {
  .avatar-uploader,
  .img-up,
  .avatar,
  .demo-image__preview {
    z-index: 999999;
    position: relative;
    display: inline-block;
    vertical-align: middle;
  }
  .img-input {
    width: 400px;
    vertical-align: middle;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  @w: 40px;
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: @w;
    height: @w;
    line-height: @w;
    text-align: center;
  }
  .avatar,
  .el-image,
  .demo-image__preview {
    width: @w;
    height: @w;
    display: inline-block;
    overflow: hidden;
    .el-image__inner {
      width: @w;
      height: @w;
    }
  }
}
</style>
